/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.boot.rabbit.domain.property;

import com.iwindplus.boot.rabbit.domain.enums.RabbitExchangeTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.compress.utils.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.List;
import java.util.Map;

/**
 * rabbit配置.
 *
 * @author zengdegui
 * @since 2020/4/24
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperty {
    /**
     * 是否开启.
     */
    @Builder.Default
    private Boolean enableConfig = true;

    /**
     * 交换机、队列、路由key.
     */
    @Builder.Default
    @NestedConfigurationProperty
    private List<Config> configs = Lists.newArrayList();

    /**
     * rabbit配置对象.
     *
     * @author zengdegui
     * @since 2020/4/24
     */
    @Data
    @SuperBuilder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Config {
        /**
         * 路由key.
         */
        private String routingKey;

        /**
         * 交换机信息（必填）.
         */
        @NestedConfigurationProperty
        private Exchange exchange;

        /**
         * 队列信息（必填）.
         */
        @NestedConfigurationProperty
        private Queue queue;

        /**
         * 交换机信息类.
         */
        @Data
        @SuperBuilder
        @NoArgsConstructor
        @AllArgsConstructor
        public static class Exchange {
            /**
             * 交换机类型（必填）.
             */
            @Builder.Default
            private RabbitExchangeTypeEnum type = RabbitExchangeTypeEnum.DIRECT;

            /**
             * 交换机名称（必填）.
             */
            private String name;

            /**
             * 是否持久化.
             */
            @Builder.Default
            private boolean durable = true;

            /**
             * 当所有绑定队列均不在使用时，是否自动删除交换机.
             */
            @Builder.Default
            private boolean autoDelete = false;

            /**
             * 交换机其他参数（可选）.
             */
            private Map<String, Object> arguments;
        }

        /**
         * 队列信息类.
         */
        @Data
        @SuperBuilder
        @NoArgsConstructor
        @AllArgsConstructor
        public static class Queue {
            /**
             * 队列名称（必填）.
             */
            private String name;

            /**
             * 是否持久化.
             */
            @Builder.Default
            private boolean durable = true;

            /**
             * 是否具有排他性.
             */
            @Builder.Default
            private boolean exclusive = false;

            /**
             * 当消费者均断开连接，是否自动删除队列.
             */
            @Builder.Default
            private boolean autoDelete = false;

            /**
             * 其他属性设置（可选）.
             */
            private Map<String, Object> arguments;

            /**
             * 绑定死信队列的交换机名称（可选，也可通过arguments设置"x-dead-letter-exchange"）
             */
            private String deadLetterExchange;

            /**
             * 绑定死信队列的路由key（可选，也可通过arguments设置"x-dead-letter-routing-key"）
             */
            private String deadLetterRoutingKey;
        }
    }
}
